<template>
  <div>
    <el-card class="main-card">
      <el-row>
        <el-col :span="9">
          <el-input
            placeholder="请输入检查项名称/项目编码"
            v-model="queryInfo.queryString"
            @clear="findPage"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="findPage"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button
            @click="insert"
            style="margin-left: 10px"
            v-has-permission="['PER_INSERT']"
            type="primary"
            >添加检查项目</el-button
          >
        </el-col>
      </el-row>
      <el-table
        :data="tableList"
        stripe
        border
        highlight-current-row
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        max-height="450"
      >
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="code" label="检查项目编码" width="180">
        </el-table-column>
        <el-table-column prop="name" label="检查项名称"> </el-table-column>
        <el-table-column prop="sex" label="适用性别" :formatter="formatSex">
        </el-table-column>
        <el-table-column prop="age" label="适用年龄"> </el-table-column>
        <el-table-column
          prop="type"
          label="检查项目类型"
          :formatter="formatType"
        >
        </el-table-column>
        <el-table-column prop="remark" label="检查项目说明"> </el-table-column>
        <el-table-column prop="attention" label="检查项目注意事项">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              @click="updateById(scope.row)"
              type="primary"
              v-has-permission="['PER_UPDATE']"
              icon="el-icon-edit"
              size="small"
            ></el-button>
            <el-button
              type="danger"
              @click="deleteById(scope.row.id)"
              v-has-permission="['PER_DELETE']"
              icon="el-icon-delete"
              size="small"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handlePageSize"
          @current-change="handlePageNumber"
          background
          :current-page="queryInfo.pageNumber"
          :page-sizes="[8, 16, 32, 64, 128]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 表单添加/修改 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      @close="dialogClose"
      width="65%"
    >
      <el-form :model="form" :rules="rules" ref="form">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="检查项编码" prop="code">
              <el-input v-model="form.code" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检查项名称" prop="name">
              <el-input v-model="form.name" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="适用性别" prop="sex">
              <el-select
                v-model="form.sex"
                placeholder="请选择性别"
                style="width: 100%"
              >
                <el-option :value="0" label="不限" />
                <el-option :value="1" label="男" />
                <el-option :value="2" label="女" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="适用年龄" prop="age">
              <el-input v-model="form.age" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检查项类型" prop="type">
              <el-select
                v-model="form.type"
                placeholder="请选择检查项类型"
                style="width: 100%"
              >
                <el-option :value="1" label="检查" />
                <el-option :value="2" label="检验" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="检查项说明">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入检查项说明"
                show-word-limit
                maxlength="100"
                :autosize="{ minRows: 2, maxRows: 5 }"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="注意事项" prop="value">
              <el-input
                v-model="form.attention"
                type="textarea"
                placeholder="请输入检查项注意事项"
                show-word-limit
                maxlength="200"
                :autosize="{ minRows: 2, maxRows: 5 }"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clickCancel">取 消</el-button>
        <el-button type="primary" @click="clickOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 分页查询条件
      queryInfo: {
        // 页码
        pageNumber: 1,
        // 页数
        pageSize: 8,
        // 关键字
        queryString: null,
      },
      // 表格数据
      tableList: [],
      // 总记录数
      total: 0,
      // 表单标题
      title: null,
      // 是否打开对话框
      // 表格加载
      loading: false,
      open: false,
      // 表单数据
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "请输入检查项目编号", trigger: "blur" },
          {
            min: 1,
            max: 16,
            message: "检查项标签的长1 到 16 个字符",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "请输入检查项名称", trigger: "blur" },
          {
            min: 1,
            max: 32,
            message: "检查项标签的长1 到 32 个字符",
            trigger: "blur",
          },
        ],
        age: [{ required: true, message: "请输入适用年龄", trigger: "blur" }],
        sex: [
          {
            required: true,
            // 自定义校验规则 使用匿名函数
            validator: (rule, value, callback) => {
              if (this.form.sex === undefined || this.form.sex === null) {
                callback(new Error("请选择性别"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            // 自定义校验规则 使用匿名函数
            validator: (rule, value, callback) => {
              if (this.form.type === undefined || this.form.type === null) {
                callback(new Error("请选择类型"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  /* 页面初始化调用方法 */
  created() {
    this.findPage();
  },
  methods: {
    /** 分页查询 */
    findPage() {
      this.loading = true;
      this.$ajax.post("/checkItem/findPage", this.queryInfo).then((res) => {
        this.loading = false;
        console.log("res.data", res.data);
        this.tableList = res.data.rows;
        this.total = res.data.total;
      });
    },
    /** 页面大小改变事件 */
    handlePageSize(newPageSize) {
      this.queryInfo.pageSize = newPageSize;
      this.findPage();
    },
    /** 页面改变事件 */
    handlePageNumber(newPageNumber) {
      // 将分页参数新数据传递给pageNumber(分页参数)
      this.queryInfo.pageNumber = newPageNumber;
      this.findPage();
    },
    /** 修改检查项信息 */
    insert() {
      this.title = "新增检查项";
      this.open = true;
    },
    /** 修改检查项信息 */
    updateById(row) {
      console.log(row);
      this.form = row;
      this.title = "修改检查项";
      this.open = true;
    },
    /** 删除检查项信息 */
    deleteById(id) {
      this.$confirm(
        "您的操作将永久删除编号为{" + id + "}的数据, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          // 调后台退出接口
          this.$ajax.delete(`/checkItem/delete/${id}`).then((res) => {
            this.$message.success(res.data.message);
            this.queryInfo.pageNumber = 1;
            this.findPage();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    /** 关闭对话框事件 */
    dialogClose() {
      // 将表单进行重置，并移除校验效果
      this.$refs.form.resetFields();
      this.findPage();
    },
    /** 点击取消 */
    clickCancel() {
      // 将表单进行重置，并移除校验效果
      this.$refs.form.resetFields();
      this.form = {};
      this.open = false;
      this.findPage();
    },
    /** 点击确定 */
    clickOk() {
      // 进行表单校验
      this.$refs.form.validate((valid) => {
        // 校验不通过
        if (!valid) {
          return this.$message.error("表单校验失败,请检查后提交！");
        }
        console.log("this.form.id", this.form.id);
        // 校验通过
        if (this.form.id === undefined || this.form.id === null) {
          this.$ajax.post("/checkItem/insert/", this.form).then((res) => {
            this.$message.success(res.data.message);
            this.open = false;
            this.findPage();
          });
        } else {
          this.$ajax.put("/checkItem/update/", this.form).then((res) => {
            this.$message.success(res.data.message);
            this.open = false;
            this.findPage();
          });
        }
      });
    },
    /** 格式化性别 */
    formatSex(row) {
      if (row.sex === "1") {
        return "男";
      } else if (row.sex === "2") {
        return "女";
      } else {
        return "不限";
      }
    },
    /** 格式化类型 */
    formatType(row) {
      if (row.type === "1") {
        return "检验";
      } else if (row.type === "2") {
        return "检查";
      }
    },
  },
};
</script>

<style scoped></style>
